package j.framework.base.controller;

import j.framework.base.service.ApplicationService;
import j.framework.core.FApplication;
import j.framework.core.Function;
import j.framework.core.LoginUser;
import j.framework.core.SysLog;
import j.util.StringUtils;
import j.util.pkgenerator.PG;
import j.util.web.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.Date;

/**
 * 页面转发控制器
 * User: jason
 * Date: 2014/11/6 11:45
 */
@WebServlet(urlPatterns = "/user/pd")
public class PageDispatcherController extends BaseController {

    @ApplicationBean
    private ApplicationService applicationService;

    /**
     * 前台传递jsp路径，由这个控制器统一转发，同时需要传递模块ID，若没有模块ID参数，则不能进入操作页面
     * 需要参数：fid=模块ID(des)&page=转发页面(DES)
     * @param view
     * @throws ServletException
     * @throws java.io.IOException
     */
    @Override
    public void doDefaultGet(HttpView view) throws ServletException, IOException {
        String fid = view.getDesId("fid");
        String page = view.getDesId("page");
        if(StringUtils.isNullOrEmpty(fid) || StringUtils.isNullOrEmpty(page)){
            throw new RuntimeException("此模块不存在！");
        }
        Function f = FApplication.getInstance().getFunctionById(fid);
        if(f != null){
            //先检测一下当前登录用户是否具备此模块的访问权限
            if(FApplication.getMembershipProvider().hasFunction(view.getUser(),f.getFunctionId())){
                view.forward(page);
            }else{
                //不具备权限
                throw new RuntimeException("您不具备["+f.getName()+"]模块的访问权限！");
            }
        }else{
            throw new RuntimeException("此模块不存在！");
        }
    }

    /**
     * 功能模块的转发，所有功能模块操作入口
     * 需要参数：act=df&fid=模块ID(DES)
     * @param view
     * @throws ServletException
     * @throws java.io.IOException
     */
    @WebRequest(action = "df")
    public void doDispatchFunction(HttpView view) throws ServletException, IOException {
        String fid = view.getDesId("fid");
        Function f = FApplication.getInstance().getFunctionById(view.getDesId("fid"));
        LoginUser user = view.getUser();
        SysLog log = new SysLog();
        log.setLogId(PG.getNext("LOG"));
        log.setOperateUserId(user.getUserId());
        log.setOperateTime(new Date());
        log.setOperateType("模块进入");
        log.setBusinessType("模块");
        log.setUserIP(WebUtils.getRequestIP(view.getRequest()));
        if(f!=null){
            log.setModuleId(f.getFunctionId());
            //先检测一下当前登录用户是否具备此模块的访问权限
            if(FApplication.getMembershipProvider().hasFunction(user,f.getFunctionId())){
                //具备权限进入模块，记录日志
                log.setLogContent("用户["+user.getName()+"]具备访问["+f.getName()+"]模块权限，允许进入！");
                applicationService.log(log);
                if(f.isJsp()){
                    view.forward(f.getUrl());
                }else{
                    view.redirect(f.getUrl());
                }
            }else{
                //不具备权限，并且记录日志
                log.setLogContent("用户["+user.getName()+"]不具备访问["+f.getName()+"]模块权限，拒绝进入！");
                applicationService.log(log);
                throw new RuntimeException("您不具备["+f.getName()+"]模块的访问权限！");
            }
        }else{
            log.setLogContent("用户["+user.getName()+"]访问的模块ID["+fid+"]不存在！");
            applicationService.log(log);
            throw new RuntimeException("此模块不存在！");
        }
    }
    @WebRequest(action = "jsp")
    public void doForwardJspPage(HttpView view) throws ServletException, IOException {
        view.forward(view.getDesId("jsp"));
    }
}
